﻿using System;
using System.Collections.Generic;
using System.Text;
//[2009-08-31 08:27:54,703] MSGOUT_LOG - 08:27:54.703 Seq[2] Ack[3] Lnk[11] OPId[DT]  = NumOfMsg==6|Msg[0]={1I011112 10242754011C007866ANV     B2000    2000    ATO   M     C     }, Msg[1]={1I011112 10242755011C007866SJS     B500     500     ATO   M     C     }, Msg[2]={1I011112 10242756011C007822AGF     B1000    1000    ATO   M     C     }, Msg[3]={1I011112 10242757011C027979REE     S5000    5000    51    M     C     }, Msg[4]={1I011112 10242758011C027979ITA     S10000   10000   42    M     C     }, Msg[5]={1I011112 10242759011C004897DHC     B3500    3500    15.7  M     C     }
namespace DOF
{
    //{1I011112 10242754011C007866ANV     B2000    2000    ATO   M     C     }    
    abstract class CTCI_Msg
    {
        public static CTCI_Msg createInstance(string str, DateTime dtOrderDate)
        {
            CTCI_Msg result = null;
            if (str.IndexOf("1I")!=-1)
            {
                try
                {
                    result = new CTCI_1I(str,dtOrderDate);
                }
                catch 
                {
                    result = null;
                }
            }
            else if (str.IndexOf("2E") != -1)
            {
                try
                {
                    result = new CTCI_2E(str, dtOrderDate);
                }
                catch
                {
                    result = null;
                }
            }
            else if (str.IndexOf("2I") != -1)
            {
                try
                {
                    result = new CTCI_2I(str, dtOrderDate);
                }
                catch
                {
                    result = null;
                }
            }
            return result;
        }
        //public abstract string getType();

    }

    class CTCI_2I : CTCI_Msg
    {
        public DateTime _OrderDate;
        public int _BuyOrderNum;
        public int _SellOrderNum;        
        public int _Vol = 0;
        public Single _Price;
        public CTCI_2I(string str, DateTime dtOrderDate)
        {
            _OrderDate = dtOrderDate;
            ParseData(str);
        }
        //{2I11 1102202614061102211014066050    28.3  74    }
        //      6       Date        Date Vol
        private void ParseData(string str)
        {            
            _BuyOrderNum = int.Parse(str.Substring(6, 8));
            _SellOrderNum = int.Parse(str.Substring(18, 8));
            _Vol = int.Parse(str.Substring(30, 4));
            string strTemp;
            int iLastPos;
            iLastPos = str.IndexOf(" ", 7);
            str = str.Substring(iLastPos).Trim();// Go to Price value
            iLastPos = str.IndexOf(" ");
            strTemp = str.Substring(0, iLastPos);
            _Price = float.Parse(strTemp);
        }

    }
    class CTCI_2E : CTCI_Msg
    {
        public DateTime _OrderDate;
        public int _OrderNum;
        public char _Side;
        public int _Vol = 0;
        public Single _Price;
        public CTCI_2E(string str, DateTime dtOrderDate)
        {
            _OrderDate = dtOrderDate;
            ParseData(str);
        }
        //{2E11 B110223541406  2300    13.5  3460  }
        //      6
        private void ParseData(string str)
        {
            _Side = str[6];
            _OrderNum = int.Parse(str.Substring(7, 8));

            string strTemp;
            int iLastPos;
            iLastPos = str.IndexOf(" ", 7);
            str = str.Substring(iLastPos).Trim();// Go to Vol value
            iLastPos = str.IndexOf(" ");
            strTemp = str.Substring(0, iLastPos);
            _Vol = int.Parse(strTemp);
            str = str.Substring(iLastPos).Trim(); // Go to Price value
            iLastPos = str.IndexOf(" ");
            strTemp = str.Substring(0, iLastPos);
            _Price = float.Parse(strTemp);       
        }

    }
    //[2010-06-14 09:01:36,656] MSGIN_LOG - Seq[946] Ack[760] Lnk[11] OPId[DT]  = NumOfMsg==1|Msg[0]={2E11 B110223541406  2300    13.5  3460  }
    class CTCI_1I : CTCI_Msg
    {
        //public int _FirmID;        
        //public string _TraderID = null;
        public DateTime _OrderDate;
        public int _OrderNum = 0;
        public string _ClientID = null;
        public string _Symbol = null;
        public char _Side;
        public int _Vol = 0;
        //public int _PublishedVol = 0;
        public string _Price = null;
        //public char _Board;
        //Filler (not use)
        //{1I011112 10242754011C007866ANV     B2000    2000    ATO   M     C     }    
        //          9       17        27      strTemp  
        public CTCI_1I(string str, DateTime dtOrderDate)
        {
            _OrderDate = dtOrderDate;
            ParseData(str);
        }
        //public override string getType()
        //{
        //    return "1I";
        //    //throw new NotImplementedException();
        //}
        private void ParseData(string str)
        {
            string strTemp = str.Substring(10, 8);//Order Number
            int iLastPos;
            _OrderNum = int.Parse(strTemp);                 
            _ClientID = str.Substring(18, 10);
            iLastPos = str.IndexOf(" ", 28);
            _Symbol = str.Substring(28, iLastPos - 28).Trim();
            strTemp = str.Substring(iLastPos).Trim();// Go to B/S char
            _Side = strTemp[0];// the first char is side.
            string[] aList = strTemp.Split(' ');
            //string s = aList[0];// B2000;            
            //_Vol = int.Parse(s.Substring(1).Trim());
            int i = 0;
            foreach( string s in aList)
            {
                if (s.Trim()!="")
                {
                    if (i == 0) _Vol = int.Parse(s.Substring(1).Trim());
                    else if (i == 2) _Price = s;// ATO or price           
                    i++;
                }
            }            
        }

    }
}
